package com.hiultra.archive.controller.phone;

import cn.hutool.core.bean.BeanUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.hiultra.archive.domain.*;
import com.hiultra.archive.service.*;
import com.hiultra.archive.utils.tool.ToolUtils;
import com.hiultra.common.core.controller.BaseController;
import com.hiultra.common.core.domain.AjaxResult;
import com.hiultra.common.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 手持机数据同步
 */
@RestController
@RequestMapping("/phone/sync")
public class PhoneController extends BaseController {


    @Autowired
    private IArchiveStorageTaskService archiveStorageTaskService;
    @Autowired
    private IArchiveStorageService archiveStorageService;
    @Autowired
    private IArchiveStorageBoxService archiveStorageBoxService;
    @Autowired
    private IArchiveBorrowTaskService archiveBorrowTaskService;
    @Autowired
    private IArchiveStocktakingTaskService archiveStocktakingTaskService;
    @Autowired
    private IArchiveStocktakingBoxService archiveStocktakingBoxService;
    @Autowired
    private IArchiveStocktakingService archiveStocktakingService;
    @Autowired
    private IArchiveBoxMoveService archiveBoxMoveService;
    @Autowired
    private IArchiveBoxMoveTaskService archiveBoxMoveTaskService;
    @Autowired
    private IArchiveUploadTaskService archiveUploadTaskService;
    @Autowired
    IArchiveUploadTaskBoxService archiveUploadTaskBoxService;
    @Autowired
    private IArchiveUploadTaskBagService archiveUploadTaskBagService;
    @Autowired
    private IArchiveUploadTaskArchiveService archiveUploadTaskArchiveService;
    @Autowired
    private IArchiveBoxinnerMoveTaskService archiveBoxinnerMoveTaskService;
    @Autowired
    private IArchiveBoxinnerMoveService archiveBoxinnerMoveService;

    /**
     * 查询档案入库列表列表
     */
    @GetMapping("/inList")
    public AjaxResult inList(ArchiveStorageTask archiveStorageTask) {
        startPage();
        List<ArchiveStorageTask> list = archiveStorageTaskService.selectArchiveStorageTaskList(archiveStorageTask);
        List<Map<String, Object>> result = new ArrayList<>();
        for (ArchiveStorageTask task : list) {
            Map<String, Object> storageMap = BeanUtil.beanToMap(task);

            // 入库单档案
            List<ArchiveStorage> archiveStorageList = archiveStorageService.selectArchiveStorageByTaskId(task.getTaskId());
            List<Map<String, Object>> archiveMapList = ToolUtils.listBeanToMap(archiveStorageList);

            storageMap.put("archiveList", archiveMapList);
            result.add(storageMap);
        }

        return AjaxResult.success(result);
    }

    /**
     * 查询借阅单
     * @param archiveBorrowTask
     * @return
     */
    @GetMapping("/borrowList")
    public TableDataInfo borrowList(ArchiveBorrowTask archiveBorrowTask) {
        startPage();
        List<ArchiveBorrowTask> list = archiveBorrowTaskService.selectArchiveBorrowTaskList(archiveBorrowTask);
        List<ArchiveBorrowTask> result = new ArrayList<>(list.size());
        for (ArchiveBorrowTask task : list) {
            ArchiveBorrowTask borrowTask = archiveBorrowTaskService.selectArchiveBorrowTaskByTaskId(task.getTaskId());
            result.add(borrowTask);
        }

        return getDataTable(result);
    }

    // 盘点列表
    @GetMapping("/checkList")
    public TableDataInfo checkList(ArchiveStocktakingTask archiveStocktakingTask){
        startPage();
        List<ArchiveStocktakingTask> stocktakingTaskList = archiveStocktakingTaskService.selectArchiveStocktakingTaskList(archiveStocktakingTask);

        for (ArchiveStocktakingTask task : stocktakingTaskList) {

            List<ArchiveStocktakingBox> stocktakingBoxList = archiveStocktakingBoxService.selectBoxByTaskId(task.getTaskId());

            for (ArchiveStocktakingBox stocktakingBox : stocktakingBoxList) {
                List<ArchiveStocktaking> stocktakingList = archiveStocktakingService.selectArchiveListByTaskBoxId(stocktakingBox.getId());
                stocktakingBox.setArchiveStocktakingList(stocktakingList);
            }
            task.setArchiveStocktakingBoxList(stocktakingBoxList);
        }

        return getDataTable(stocktakingTaskList);
    }

    // 档案盒移位
    @GetMapping("/boxMoveList")
    public TableDataInfo boxMoveList(ArchiveBoxMoveTask archiveBoxMoveTask){
        startPage();
        List<ArchiveBoxMoveTask> list = archiveBoxMoveTaskService.selectArchiveBoxMoveTaskList(archiveBoxMoveTask);
        List<Map> result = new ArrayList<>();
        for (ArchiveBoxMoveTask task : list) {
            List<ArchiveBoxMove> archiveBoxMoves = archiveBoxMoveService.selectArchiveBoxMoveByTaskId(task.getTaskId());
            Map taskMap = BeanUtil.beanToMap(task);
            taskMap.put("boxList", archiveBoxMoves);
            result.add(taskMap);
        }

        return getDataTable(result);
    }



    // 档案移位
    @GetMapping("/archiveMoveList")
    public TableDataInfo archiveMoveList(ArchiveBoxinnerMoveTask archiveBoxinnerMoveTask){
        startPage();
        List<ArchiveBoxinnerMoveTask> list = archiveBoxinnerMoveTaskService.selectArchiveBoxinnerMoveTaskList(archiveBoxinnerMoveTask);

        List<Map> result = new ArrayList<>();
        for (ArchiveBoxinnerMoveTask task : list) {
            List<ArchiveBoxinnerMove> archiveBoxinnerMoves = archiveBoxinnerMoveService.selectArchiveBoxinnerMoveByTaskId(task.getTaskId());
            Map taskMap = BeanUtil.beanToMap(task);
            taskMap.put("archiveList", archiveBoxinnerMoves);
            result.add(taskMap);
        }

        return getDataTable(result);
    }

    // 转存
    @GetMapping("/moveList")
    public TableDataInfo moveList(ArchiveBoxMoveTask archiveBoxMoveTask) throws ParseException, JsonProcessingException {
        startPage();
        List<ArchiveUploadTask> list = archiveUploadTaskService.selectArchiveUploadTaskList(new ArchiveUploadTask());
        List<Map<String, Object>> result = new ArrayList<>(list.size());
        for (ArchiveUploadTask task : list) {
            // 档案盒
            ArchiveUploadTaskBox taskBox = new ArchiveUploadTaskBox();
            taskBox.setTaskId(task.getTaskId());
            List<ArchiveUploadTaskBox> archiveBoxMoves = archiveUploadTaskBoxService.selectArchiveUploadTaskBoxList(taskBox);
            Map<String, Object> taskMap = BeanUtil.beanToMap(task);
            taskMap.put("boxList", archiveBoxMoves);
            // 档案袋
            ArchiveUploadTaskBag archiveUploadTaskBag = new ArchiveUploadTaskBag();
            archiveUploadTaskBag.setTaskId(task.getTaskId());
            List<ArchiveUploadTaskBag> taskBagList = archiveUploadTaskBagService.selectArchiveUploadTaskBagList(archiveUploadTaskBag);
            taskMap.put("bagList", taskBagList);
            // 档案
            ArchiveUploadTaskArchive taskArchive = new ArchiveUploadTaskArchive();
            taskArchive.setTaskId(task.getTaskId());
            List<ArchiveUploadTaskArchive> archiveList = archiveUploadTaskArchiveService.selectArchiveUploadTaskArchiveList(taskArchive);
            taskMap.put("archiveList", archiveList);

            result.add(taskMap);
        }

        return getDataTable(result);
    }
}
